import { _decorator, Collider, Color, Component, ConfigurableConstraint, director, instantiate, Label, Node, Sprite, sys } from 'cc';
import { HttpCom } from '../net/HttpCom';
import { GlobalVar } from '../GlobalVar';
const { ccclass, property } = _decorator;
const TG_BOT_NAME = "LastOasis_bot"
const TG_BOT_APP = "LastOasis"
const projectUrl = "https://game.lastoasis.org/"
@ccclass('InviteFriends')
export class InviteFriends extends Component {

    @property(Label)
    inviteCountLab: Label = null
    @property(Label)
    rewardInviteCountLab: Label = null;

    @property([Node])
    ivtiveIcons: Node [] = [];
    @property(Node)
    childrenList: Node = null;
    @property(Node)
    childrenListContent: Node = null;

    @property(Node)
    childrenItem: Node = null;

    start() {
       this.childrenList.active = false;
    }

    update(deltaTime: number) {
        
    }

    closeChildrenList(){
        this.childrenList.active = false;
    }

    onInviteBtnClick(){
        console.log("邀请好友");
        let url = "http://t.me/LastOasis_bot/LastOasis?inviteCode=" + GlobalVar.userInfo.inviteCode

        if (window['Telegram'] && window['Telegram'].WebApp){
            //window['Telegram'].WebApp.openTelegramLink("https://t.me/share/url?url="+url)

            const link = `https://t.me/${TG_BOT_NAME}/${TG_BOT_APP}?startapp=${btoa(
                JSON.stringify({ inviteCode: GlobalVar.userInfo.inviteCode})
              )}`;
          
          const shareLink =  `https://t.me/share/url?url=${encodeURIComponent(link)}`;
          window['Telegram'].WebApp.openTelegramLink(shareLink)
        }else{

            sys.openURL("https://t.me/share/url?url="+url)
        }
    }

    getChildList(){
        this.childrenList.active = true;
        this.childrenListContent.removeAllChildren();
        HttpCom.getChildList((resp)=>{
            let rows = resp.rows;

            rows.forEach((e,i)=>{
                let child = instantiate(this.childrenItem);
                child.getComponent(Label).string = i+1 + ". " + e.username 
                this.childrenListContent.addChild(child);
                child.active = true;
            })
        })
    }

    protected onEnable(): void {
        HttpCom.getChildList((resp)=>{

            let rows = resp.rows;
            rows.forEach((e,i)=>{
                
            })
            if(rows.length == 0){
                return;
            }   

            this.inviteCountLab.string = rows.length.toString();
            //let a = 6
            let count = rows.length % 3;
            //count = a % 3;
            console.log(count,"count");
            if(count == 1){
                this.ivtiveIcons[0].getComponent(Sprite).color = new Color(255,255,255);    
                this.ivtiveIcons[1].getComponent(Sprite).color = new Color(80,80,80);    
                this.ivtiveIcons[2].getComponent(Sprite).color = new Color(80,80,80);    
                
            }
            if(count == 2){
                   
                this.ivtiveIcons[2].getComponent(Sprite).color = new Color(80,80,80);    
                 
                this.ivtiveIcons[0].getComponent(Sprite).color = new Color(255,255,255);   
                this.ivtiveIcons[1].getComponent(Sprite).color = new Color(255,255,255);   
            }
            if(count == 0){
                this.ivtiveIcons[0].getComponent(Sprite).color = new Color(255,255,255);
                this.ivtiveIcons[1].getComponent(Sprite).color = new Color(255,255,255);
                this.ivtiveIcons[2].getComponent(Sprite).color = new Color(255,255,255);
                    
            }

        })
    }
}


